<template>
  <div class="register-page">
    <van-nav-bar 
      title="注册" 
      left-arrow 
      fixed 
      placeholder 
      @click-left="router.back()" 
      class="custom-nav"
    />
    <div class="register-content">
      <div class="register-header">
        <img src="@/assets/images/logo.png" alt="logo" class="logo">
        <h2 class="welcome">创建账号</h2>
        <p class="subtitle">请填写以下信息完成注册</p>
      </div>

      <van-form @submit="onSubmit" ref="formRef">
        <div class="register-form">
          <div class="input-wrapper">
            <van-field name="phone" v-model="formData.phone" type="tel" placeholder="请输入手机号" class="custom-field" :rules="phoneRules"
              :border="false">
              <template #left-icon>
                <van-icon name="phone-o" />
              </template>
            </van-field>
            <van-field name="sms" v-model="formData.sms" center placeholder="请输入验证码" class="custom-field" :rules="codeRules"
              :border="false">
              <template #left-icon>
                <van-icon name="shield-o" />
              </template>
              <template #button>
                <van-button size="small" type="primary" class="code-btn"
                  :disabled="!!countdown || !formData.phone || !/^1[3-9]\d{9}$/.test(formData.phone)" @click="sendSms">
                  {{ countdown ? `${countdown}s后重新获取` : '获取验证码' }}
                </van-button>
              </template>
            </van-field>
            <van-field 
              name="password" 
              v-model="formData.password" 
              :type="showPassword ? 'text' : 'password'" 
              placeholder="请设置密码" 
              class="custom-field"
              :rules="passwordRules" 
              :border="false"
            >
              <template #left-icon>
                <van-icon name="lock" />
              </template>
              <template #right-icon>
                <van-icon 
                  :name="showPassword ? 'eye-o' : 'closed-eye'" 
                  @click="showPassword = !showPassword"
                  class="eye-icon"
                />
              </template>
            </van-field>
            <van-field 
              name="confirmPassword" 
              v-model="formData.confirmPassword" 
              :type="showConfirmPassword ? 'text' : 'password'" 
              placeholder="请确认密码" 
              class="custom-field"
              :rules="confirmPasswordRules" 
              :border="false"
            >
              <template #left-icon>
                <van-icon name="lock" />
              </template>
              <template #right-icon>
                <van-icon 
                  :name="showConfirmPassword ? 'eye-o' : 'closed-eye'" 
                  @click="showConfirmPassword = !showConfirmPassword"
                  class="eye-icon"
                />
              </template>
            </van-field>
          </div>

          <van-button block round type="primary" native-type="submit" class="submit-btn" :loading="loading">
            注册
          </van-button>

          <div class="action-links">
            <span class="link" @click="router.push('/auth/login')">已有账号？去登录</span>
          </div>
        </div>
      </van-form>

      <!-- <div class="other-register">
        <div class="divider">其他注册方式</div>
        <div class="register-options">
          <button class="option-btn" @click="router.push('/auth/email-register')">
            邮箱注册
          </button>
        </div>
      </div> -->
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { useUserStore } from '@/stores/user'
import { phoneRules, codeRules, passwordRules, generateConfirmPasswordRules } from '@/utils/validate'
import { useCountdown } from '@/composables/useCountdown'

const router = useRouter()
const userStore = useUserStore()
const formRef = ref()
const { countdown, startCountdown } = useCountdown()

const formData = reactive({
  phone: '13800138000',
  sms: '',
  password: '',
  confirmPassword: ''
})

const loading = ref(false)

const confirmPasswordRules = computed(() =>
  generateConfirmPasswordRules(formData.password)
)

const showPassword = ref(false)
const showConfirmPassword = ref(false)

const sendSms = async () => {
  try {
    await formRef.value?.validate('phone')
    await userStore.sendSms(formData.phone)
    showToast('验证码已发送')
    startCountdown()
  } catch (error: any) {
    showToast(error?.message || '发送失败')
  }
}

const onSubmit = async () => {
  try {
    loading.value = true
    await userStore.register({
      phone: formData.phone,
      sms: formData.sms,
      password: formData.password
    })
    showToast('注册成功')
    router.push('/auth/login')
  } catch (error: any) {
    showToast(error.message || '注册失败')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.register-page {
  min-height: 100vh;
  background: linear-gradient(to bottom, #F8E6FF, #F0E6FF);

  .custom-nav {
    background: transparent;

    :deep(.van-nav-bar__title) {
      color: #333;
      font-size: 18px;
      font-weight: 500;
    }

    :deep(.van-icon) {
      color: #333;
      font-size: 20px;
    }
  }
}

.register-content {
  padding: 30px 20px 30px; // 顶部padding增加，为导航栏留出空间
}

.register-header {
  text-align: center;
  margin-bottom: 25px;

  .logo {
    width: 72px;
    height: 72px;
    border-radius: 16px;
    margin-bottom: 16px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  .welcome {
    font-size: 22px;
    font-weight: 600;
    color: #333;
  }

  .subtitle {
    font-size: 14px;
    color: #666;
  }
}

.register-form {
  margin-bottom: 45px;
  .input-wrapper {
    background: rgba(255, 255, 255, 0.9);
    border-radius: 16px;
    padding: 20px;
    margin-bottom: 25px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    position: relative;
    :deep(.custom-field) {
      background: transparent;
      padding: 5px 0;
      border-bottom: 1px solid rgba(0, 0, 0, 0.08);
      &.van-cell {
        overflow: visible;
      }

      .van-field__error-message {
        position: absolute;
        left: 0px; // 与输入框文本对齐
        bottom: -7px;
        font-size: 12px;
        // color: #ff4646; // 更柔和的红色
        white-space: nowrap; // 防止文字换行
      }

      // 输入框获得焦点时的样式
      &.van-field--error {
        .van-field__control {
          color: #ff4646;
        }

        .van-field__left-icon .van-icon {
          color: #ff4646;
        }
      }

      &:last-child {
        margin-bottom: 0;
        border-bottom: none;
      }

      .van-field__control {
        font-size: 16px;
        color: #333;
        height: 48px;
        padding-left: 8px;

        &::placeholder {
          color: #999;
          font-size: 15px;
        }
      }

      .van-field__left-icon {
        display: flex;
        align-items: center;
        margin-right: 4px;

        .van-icon {
          font-size: 20px;
          color: #666;
        }
      }

      .code-btn {
        height: 36px;
        padding: 0 16px;
        border-radius: 18px;
        background: $primary-color !important;
        border: none;
        font-size: 14px;
      }
    }
  }

  .submit-btn {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 500;
    background: #8A2BE2;
    border: none;
    margin: 30px 0 24px;
  }

  .action-links {
    display: flex;
    justify-content: center;
    padding: 0 8px;

    .link {
      color: #8A2BE2;
      font-size: 14px;
      cursor: pointer;
      padding: 4px;

      &:active {
        opacity: 0.7;
      }
    }
  }
}

</style>



